package no.sachmuld.methodcombination.api;

import java.util.Arrays;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;

/**
 *
 */
public class BehaviourChain<T> {

    private Deque<T> chain = new LinkedList<>();

    public void add(T behaviour) {
        chain.add(behaviour);
    }

    public void addAll(T... behaviours) {
        chain.addAll(Arrays.asList(behaviours));
    }

    public void push(T behaviour) {
        chain.push(behaviour);
    }

    public T pop() {
        return chain.pop();
    }

    public Iterable<T> topDown() {
        return new Iterable<T>() {
            @Override
            public Iterator<T> iterator() {
                return chain.iterator();
            }
        };
    }

    public Iterable<T> bottomUp() {
        return new Iterable<T>() {
            @Override
            public Iterator<T> iterator() {
                return chain.descendingIterator();
            }
        };
    }

    public Iterable<T> iterable(Direction direction) {
        if (direction == Direction.BOTTOM_UP) {
            return bottomUp();
        } else if (direction == Direction.TOP_DOWN) {
            return topDown();
        } else {
            return null;
        }
    }

}
